package com.alinu.workbench.service.impl;

import com.alinu.utils.DateTimeUtil;
import com.alinu.utils.SqlSessionUtil;
import com.alinu.utils.StringUtil;
import com.alinu.utils.UUIDUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.Customer;
import com.alinu.workbench.domain.Tran;
import com.alinu.workbench.domain.TranHistory;
import com.alinu.workbench.mapper.CustomerMapper;
import com.alinu.workbench.mapper.TranHistoryMapper;
import com.alinu.workbench.mapper.TranMapper;
import com.alinu.workbench.service.TranService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/20
 * @description
 */
public class TranServiceImpl implements TranService {
    private final TranMapper tranMapper = SqlSessionUtil.getSqlSession().getMapper(TranMapper.class);
    private final TranHistoryMapper historyMapper = SqlSessionUtil.getSqlSession().getMapper(TranHistoryMapper.class);
    private final CustomerMapper customerMapper = SqlSessionUtil.getSqlSession().getMapper(CustomerMapper.class);

    @Override
    public boolean createSimpleTransaction(Tran tran) {
        boolean flag = true;
        int count = tranMapper.createSimpleTransaction(tran);

        if (count != 1) {
            System.out.println("创建交易失败");
            flag = false;
        }

        TranHistory history = new TranHistory();
        history.setId(UUIDUtil.getUUID());
        history.setCreateBy(tran.getCreateBy());
        history.setCreateTime(tran.getCreateTime());
        history.setExpectedDate(tran.getExpectedDate());
        history.setMoney(tran.getMoney());
        history.setStage(tran.getStage());
        history.setTranId(tran.getId());

        int count2 = historyMapper.saveTransactionHistory(history);

        if (count2 != 1) {
            System.out.println("创建交易历史失败");
            flag = false;
        }

        return flag;
    }

    @Override
    public PaginationVO<Tran> getTransList(Map<String, Object> params) {

        int total = tranMapper.getTotal(params);

        List<Tran> tranList = tranMapper.getTransList(params);

        PaginationVO<Tran> dataList = new PaginationVO<>();
        dataList.setDataList(tranList);
        dataList.setTotal(total);

        return dataList;
    }

    @Override
    public boolean createTransaction(Tran tran, String customerName) {
        boolean flag = true;

        // 先根据客户名称查询客户
        String customerId = customerMapper.findCustomerIdByName(customerName);

        if (StringUtil.isEmpty(customerId)) {
            customerId = UUIDUtil.getUUID();
            Customer customer = new Customer();
            customer.setId(customerId);
            customer.setCreateBy(tran.getCreateBy());
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setName(customerName);
            customer.setNextContactTime(tran.getNextContactTime());
            customer.setContactSummary(tran.getContactSummary());
            customer.setDescription(tran.getDescription());
            customer.setOwner(tran.getOwner());
            customer.setWebsite("");
            customer.setPhone("");
            customer.setAddress("");

            customerMapper.saveCustomer(customer);
        }

        tran.setCustomerId(customerId);

        // 创建一个交易
        int count = tranMapper.createTransaction(tran);

        if (count != 1) {
            System.out.println("创建交易失败");
            flag = false;
        }

        // 创建一条交易历史
        TranHistory history = new TranHistory();
        history.setId(UUIDUtil.getUUID());
        history.setCreateBy(tran.getCreateBy());
        history.setCreateTime(tran.getCreateTime());
        history.setExpectedDate(tran.getExpectedDate());
        history.setMoney(tran.getMoney());
        history.setStage(tran.getStage());
        history.setTranId(tran.getId());
        history.setPossibility(tran.getPossibility());

        int count2 = historyMapper.saveTransactionHistory(history);

        if (count2 != 1) {
            System.out.println("创建交易历史失败");
            flag = false;
        }

        return flag;
    }

    @Override
    public Tran findTranById(String id) {
        return tranMapper.findTranById(id);
    }

    @Override
    public List<TranHistory> getTranHistoryList(String tranId) {

        return historyMapper.getTranHistoryList(tranId);
    }

    @Override
    public boolean changeStage(Tran tran) {
        int count = tranMapper.changeStage(tran);

        TranHistory tranHistory = new TranHistory();
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setCreateBy(tran.getEditBy());
        tranHistory.setCreateTime(tran.getEditTime());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setTranId(tran.getId());
        tranHistory.setStage(tran.getStage());
        tranHistory.setPossibility(tran.getPossibility());

        int count2 = historyMapper.saveTransactionHistory(tranHistory);


        return count == 1 && count2 == 1;
    }

    @Override
    public Map<String, Object> getTransCharts() {
        // 查询总条数
        int total = tranMapper.getTotalCharts();
        //
        List<Map<String, Object>> dataList = tranMapper.getTransCharts();

        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("total", total);
        resultMap.put("dataList", dataList);

        return resultMap;
    }
}
